# import gradio as gr
# import scipy.io as scio
# import numpy as np
# import matplotlib.pyplot as plt
# import io
# import base64
# from scipy.fftpack import fft
# from sklearn.decomposition import PCA
#
# def process_file(file):
#     data = scio.loadmat(file.name)
#     label = data["label"]
#     y1 = data["data1"]
#     phi1 = data["phi1"]
#     y2 = data["data2"]
#     phi2 = data["phi2"]
#
#     x_data1 = [np.dot(np.linalg.pinv(phi1[i]), y1[i]) for i in range(len(label))]
#     x_data2 = [np.dot(np.linalg.pinv(phi2[i]), y2[i]) for i in range(len(label))]
#
#     x_data1_real = np.real(x_data1).reshape(-1, 1)
#     x_data2_real = np.real(x_data2).reshape(-1, 1)
#     x_data1_imag = np.imag(x_data1).reshape(-1, 1)
#     x_data2_imag = np.imag(x_data2).reshape(-1, 1)
#
#     x_data3_real = np.concatenate((x_data1_real, x_data2_real), axis=1)
#     x_data3_imag = np.concatenate((x_data1_imag, x_data2_imag), axis=1)
#
#     pca = PCA(1)
#     x_data_real = pca.fit_transform(x_data3_real).reshape(1 * 195 * 101, 1)
#     x_data_imag = pca.fit_transform(x_data3_imag).reshape(1 * 195 * 101, 1)
#
#     x_data = np.array([complex(x_data_real[j, 0], x_data_imag[j, 0]) for j in range(len(x_data_imag))]).reshape(1, 195, 101)
#     x_data_fft = fft(x_data)
#     x_data_real = np.real(x_data_fft)
#     x_data_img = np.imag(x_data_fft)
#     x_final = np.stack((x_data_real, x_data_img), axis=3)
#
#     fig, ax = plt.subplots(2, 1, figsize=(10, 12))
#     ax[0].plot(x_data3_real)
#     ax[0].set_title("Pre-processed Data")
#     ax[1].plot(x_final[0, :, :, 0])
#     ax[1].set_title("Post-processed Data")
#
#     buf = io.BytesIO()
#     plt.savefig(buf, format='png')
#     buf.seek(0)
#     img_base64 = base64.b64encode(buf.read()).decode('utf-8')
#     plt.close()
#
#     return img_base64
#
# iface = gr.Interface(
#     fn=process_file,
#     inputs=gr.components.File(label="Upload .mat file", type="filepath"),
#     outputs=gr.components.Image(type="numpy", label="Processed Image"),
#     title="Spectrum Analysis",
#     description="Upload a .mat file to analyze the spectrum."
# )
#
# iface.launch()

import gradio as gr
import scipy.io as scio
import numpy as np
import matplotlib.pyplot as plt
import io
import base64
from scipy.fftpack import fft
from sklearn.decomposition import PCA

def display_images(file):
    data = scio.loadmat(file.name)
    label = data["label"]
    y1 = data["data1"]
    phi1 = data["phi1"]
    y2 = data["data2"]
    phi2 = data["phi2"]

    x_data1 = [np.dot(np.linalg.pinv(phi1[i]), y1[i]) for i in range(len(label))]
    x_data2 = [np.dot(np.linalg.pinv(phi2[i]), y2[i]) for i in range(len(label))]

    x_data1_real = np.real(x_data1).reshape(-1, 1)
    x_data2_real = np.real(x_data2).reshape(-1, 1)
    x_data1_imag = np.imag(x_data1).reshape(-1, 1)
    x_data2_imag = np.imag(x_data2).reshape(-1, 1)

    x_data3_real = np.concatenate((x_data1_real, x_data2_real), axis=1)
    x_data3_imag = np.concatenate((x_data1_imag, x_data2_imag), axis=1)

    fig, ax = plt.subplots(2, 1, figsize=(10, 12))
    ax[0].plot(x_data3_real)
    ax[0].set_title("Pre-processed Data")
    ax[1].plot(x_data3_imag)
    ax[1].set_title("Imaginary Data")

    buf = io.BytesIO()
    plt.savefig(buf, format='png')
    buf.seek(0)
    img_base64 = base64.b64encode(buf.read()).decode('utf-8')
    plt.close()

    return img_base64, img_base64

def process_data(file):
    data = scio.loadmat(file.name)
    label = data["label"]
    y1 = data["data1"]
    phi1 = data["phi1"]
    y2 = data["data2"]
    phi2 = data["phi2"]

    x_data1 = [np.dot(np.linalg.pinv(phi1[i]), y1[i]) for i in range(len(label))]
    x_data2 = [np.dot(np.linalg.pinv(phi2[i]), y2[i]) for i in range(len(label))]

    x_data1_real = np.real(x_data1).reshape(-1, 1)
    x_data2_real = np.real(x_data2).reshape(-1, 1)
    x_data1_imag = np.imag(x_data1).reshape(-1, 1)
    x_data2_imag = np.imag(x_data2).reshape(-1, 1)

    x_data3_real = np.concatenate((x_data1_real, x_data2_real), axis=1)
    x_data3_imag = np.concatenate((x_data1_imag, x_data2_imag), axis=1)

    pca = PCA(1)
    x_data_real = pca.fit_transform(x_data3_real).reshape(1 * 195 * 101, 1)
    x_data_imag = pca.fit_transform(x_data3_imag).reshape(1 * 195 * 101, 1)

    x_data = np.array([complex(x_data_real[j, 0], x_data_imag[j, 0]) for j in range(len(x_data_imag))]).reshape(1, 195, 101)
    x_data_fft = fft(x_data)
    x_data_real = np.real(x_data_fft)
    x_data_img = np.imag(x_data_fft)
    x_final = np.stack((x_data_real, x_data_img), axis=3)

    return np.mean(x_final), x_final[0, :, :, 0].tolist(), x_final[0, :, :, 1].tolist()

with gr.Blocks() as demo:
    file_input = gr.File(label="Upload .mat file")
    img1 = gr.Image()
    img2 = gr.Image()
    mean_output = gr.Number()
    array1_output = gr.Dataframe()
    array2_output = gr.Dataframe()
    submit_btn = gr.Button("Submit")

    file_input.change(display_images, inputs=file_input, outputs=[img1, img2])
    submit_btn.click(process_data, inputs=file_input, outputs=[mean_output, array1_output, array2_output])

demo.launch()